<?php namespace App\Models;
use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Model;


class ProductModel extends Model 

{

protected $table      = 'products';
protected $primaryKey = 'Id';
protected $allowedFields = ['image','imageTitle','slug','category','info','price'];
protected $limit;
protected $offset;
protected $Id;
protected $imageTitle;
protected $slug;
protected $category;
protected $info;
	
/*
$db      = \Config\Database::connect();
$builder = $db->table('products');	
	*/
	
public function insertProduct($image,$imageTitle,$slug,$category,$info,$price)

	{
	$data = [
		'image' => $image,
		'imageTitle'    => $imageTitle,
		'slug'=> $slug,
		'category'=>$category,
		'info'=>$info,
		'price'=>$price
	];

	$this->save($data);



	}	
	
	
	public function getAll()

	{
	return $this->orderBy('Id', 'desc')->findAll();
	}
	
	
	
	
	
	
	
	
	
	
	

   public function getOne($id)
       { 
       
      return $this->find($id);
       
      // return $this->asArray()
      // ->where(['Id'=>$id]);
       
       }
	
	
	public function amendProduct($Id,$imageTitle,$slug,$category,$info,$price)
     
     
     {
     
    
     $data =[
     'imageTitle'=>$imageTitle,
     'slug'=>$slug,
     'category'=>$category,
     'info'=>$info,
     'price'=>$price
     ];
   $logic=   $this->update($Id,$data);
   return $logic;
       
     }
	
	 public function deleteOne($id)
       {
       $this->where('Id',$id )->delete();
       //need to get name image associated with blog and unlink it 
       
       return True;
       
       
       }
	 public function getCountByCategory($category)
    
    {
          $db      = \Config\Database::connect();
              $builder = $db->table('products');
          
         $query = $builder->where('category',$category)->countAllResults();
         return $query;
/*
		   $query = $db->query("SELECT COUNT(*) FROM products where category=$category  ");
		   $count   = $query->getRowArray();
                return  $count;
*/
		
		}
	
	public function useCategory($category)
	{
	echo $category;	
		
	$query= $this->where('category', $category);
	//note fixed issue by removing findall() at end after category 
      return $query;          
	}
}
